package com.aletto.general_store.ui.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aletto.general_store.R;
import com.aletto.general_store.configs.SubscribeEventBus;

import com.aletto.general_store.data.entities.WalletAccount;

import com.aletto.general_store.data.model.ConsumerOrder;
import com.aletto.general_store.data.model.StoreOrder;
import com.aletto.general_store.presenters.OrderNumberTemplate;
import com.aletto.general_store.presenters.UserDetailsTemplate;

import com.aletto.general_store.presenters.views.OrderGoodsBuyView;
import com.aletto.general_store.ui.activities.ApproveActivity;
import com.aletto.general_store.ui.activities.WalletAccountActivity;
import com.aletto.general_store.ui.activities.orders.ConsumerOrderActivity;

import com.aletto.general_store.ui.activities.orders.StoreOrderActivity;
import com.aletto.general_store.utils.LocalConfigStore;
import com.aletto.general_store.ui.activities.BrowserHistoryActivity;
import com.aletto.general_store.ui.activities.FavoritesActivity;
import com.aletto.general_store.ui.activities.SettingsActivity;
import com.aletto.general_store.ui.activities.WalletImportActivity;
import com.aletto.general_store.ui.activities.WalletReceiveActivity;
import com.aletto.general_store.utils.Login;
import com.aletto.general_store.utils.event.ExitEvent;
import com.aletto.general_store.utils.event.LoginEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.Subscribe;


/**
 * 我的
 */
public class MineFragment extends Fragment implements SubscribeEventBus {

    private Button mLoginButton;
    private ViewGroup mUserProfileLayout;
//    private ImageView mAvatarImageView;
    private ViewGroup layoutHead;
    private TextView mWalletNameTextView;

    private ViewGroup layout_parent;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 获取用户信息
        mUserProfileLayout = view.findViewById(R.id.user_profile_layout);
        layoutHead = view.findViewById(R.id.layout_head);
        new UserDetailsTemplate(layoutHead).execute(getLifecycle());

        // 订单信息
        layout_parent = view.findViewById(R.id.layout_parent);
        new OrderNumberTemplate(layout_parent).execute(getLifecycle());

        // ------------------- 用户信息 -------------------
        TextView text_view_BNB = view.findViewById(R.id.text_view_BNB);
        TextView text_view_USDT = view.findViewById(R.id.text_view_USDT);
        TextView text_view_0X = view.findViewById(R.id.text_view_0X);

        text_view_BNB.setText("0.0000 BNB");
        text_view_USDT.setText("0.00 USDT");
        text_view_0X.setText("0.00 0xB");

        TextView tv_signature = view.findViewById(R.id.tv_signature);
        tv_signature.setText(R.string.not_set_sign_tips);

//        mAvatarImageView = view.findViewById(R.id.iv_avatar);
        mLoginButton = view.findViewById(R.id.button_login);
        mWalletNameTextView = view.findViewById(R.id.tv_wallet_name);

        renderUserProfile();

        mLoginButton.setOnClickListener(v ->
                startActivity(new Intent(v.getContext(), WalletImportActivity.class)));

        // ------------------- 事件跳转 -------------------
        view.findViewById(R.id.tv_favorites).setOnClickListener(new Login(v -> // 收藏
                startActivity(new Intent(v.getContext(), FavoritesActivity.class))));

        view.findViewById(R.id.tv_view_history).setOnClickListener(new Login(v -> // 浏览历史
                startActivity(new Intent(v.getContext(), BrowserHistoryActivity.class))));

        view.findViewById(R.id.tv_settings).setOnClickListener(v -> // 设置
                startActivity(new Intent(v.getContext(), SettingsActivity.class)));

        view.findViewById(R.id.image_view_1).setOnClickListener(v -> // 收款
                startActivity(new Intent(v.getContext(), WalletReceiveActivity.class)));
        view.findViewById(R.id.image_view_2).setOnClickListener(v -> // 收款
                startActivity(new Intent(v.getContext(), WalletReceiveActivity.class)));
        view.findViewById(R.id.image_view_3).setOnClickListener(v -> // 收款
                startActivity(new Intent(v.getContext(), WalletReceiveActivity.class)));

        view.findViewById(R.id.tv_approve).setOnClickListener(new Login(v -> // approve
                startActivity(new Intent(v.getContext(), ApproveActivity.class))));

//        view.findViewById(R.id.tv_signature).setOnClickListener(v -> // 个性签名
//                startActivity(new Intent(v.getContext(), ProfileActivity.class)));

        // item_store_delivery 待发货
        // item_trade_dustbin  待收货

        // ------------------- 店铺 -------------------
        TextView tv_store_delivery_number = view.findViewById(R.id.tv_store_delivery_number);
        TextView tv_store_shipped_number = view.findViewById(R.id.tv_store_shipped_number);
        TextView tv_store_after_sale_number = view.findViewById(R.id.tv_store_after_sale_number);
        TextView tv_store_complete_number = view.findViewById(R.id.tv_store_complete_number);
        tv_store_delivery_number.setText("0");
        tv_store_shipped_number.setText("0");
        tv_store_complete_number.setText("0");
        tv_store_after_sale_number.setText("0");

        // ------------------- 订单跳转 -------------------
        view.findViewById(R.id.tv_all_consumer_order).setOnClickListener(new Login(v -> // 我的交易全部订单
                v.getContext().startActivity(new Intent(v.getContext(), ConsumerOrderActivity.class))));
        view.findViewById(R.id.trade_delivery_layout).setOnClickListener(new Login(v ->
                v.getContext().startActivity(ConsumerOrderActivity.newIntent(v.getContext(), ConsumerOrder.delivery))));
        view.findViewById(R.id.shipped_layout).setOnClickListener(new Login(v ->
                v.getContext().startActivity(ConsumerOrderActivity.newIntent(v.getContext(), ConsumerOrder.dustbin))));
        view.findViewById(R.id.tv_comment).setOnClickListener(new Login(v ->
                v.getContext().startActivity(ConsumerOrderActivity.newIntent(v.getContext(), ConsumerOrder.comment))));
        view.findViewById(R.id.trade_subsequent_layout).setOnClickListener(new Login(v ->
                v.getContext().startActivity(ConsumerOrderActivity.newIntent(v.getContext(), ConsumerOrder.refund))));

        view.findViewById(R.id.tv_all_store_order).setOnClickListener(new Login(v -> // 我的店铺全部订单
                v.getContext().startActivity(new Intent(v.getContext(), StoreOrderActivity.class))));
        view.findViewById(R.id.store_delivery_layout).setOnClickListener(new Login(v -> // 待发货
                startActivity(StoreOrderActivity.newIntent(v.getContext(), StoreOrder.DELIVERY_TYPE))));
        view.findViewById(R.id.layout_store_shipped).setOnClickListener(new Login(v -> // 已发货
                startActivity(StoreOrderActivity.newIntent(v.getContext(), StoreOrder.SHIPPED_TYPE))));
        view.findViewById(R.id.layout_store_after_sale).setOnClickListener(new Login(v -> // 售后中
                startActivity(StoreOrderActivity.newIntent(v.getContext(), StoreOrder.AFTER_SALE_TYPE))));
        view.findViewById(R.id.layout_store_complete).setOnClickListener(new Login(v -> // 已完成
                startActivity(StoreOrderActivity.newIntent(v.getContext(), StoreOrder.COMPLETE_TYPE))));

    }

    @Override
    public void onResume() {
        super.onResume();
        // 获取用户信息
        new UserDetailsTemplate(layoutHead).execute(getLifecycle());

        // 订单信息
        new OrderNumberTemplate(layout_parent).execute(getLifecycle());
    }

    private void renderUserProfile() {
        if (!LocalConfigStore.getInstance().isLogin()) {
            mLoginButton.setVisibility(View.VISIBLE);
            mUserProfileLayout.setVisibility(View.INVISIBLE);
            return;
        }

        mLoginButton.setVisibility(View.GONE);
        mUserProfileLayout.setVisibility(View.VISIBLE);
        WalletAccount wallet = LocalConfigStore.getInstance().getWallet();

        if (wallet != null) {
            mWalletNameTextView.setOnClickListener(v ->
                    startActivity(new Intent(v.getContext(), WalletAccountActivity.class)));
        }
    }


    @Subscribe
    public void onEvent(LoginEvent event) {
        // renderUserProfile();
        new UserDetailsTemplate(layoutHead).execute(getLifecycle());

        // 订单信息
        new OrderNumberTemplate(layout_parent).execute(getLifecycle());
    }

    @Subscribe
    public void onEvent(ExitEvent event){
        LocalConfigStore.getInstance().clearToken();
        LocalConfigStore.getInstance().clearWallet();

        renderUserProfile();
    }

}
